import { QUIView } from "./QUIView";
import QGamePlugin from "./QGamePlugin";
export type QUIVerticalLayoutConfig = {
    id?: string;
    class?: string;
    widthRatio?: number;
    heightRatio?: number;
    width?: number;
    height?: number;
    miniWidth?: number;
    miniHeight?: number;
    maxWidth?: number;
    maxHeight?: number;
    left?: number;
    top?: number;
    background?: number | string;
    borderColor?: number | string;
    borderWidth?: number;
    aplpha?: number;
};

/**
 * 垂直布局
 *
 * 每个子元素独占一行，宽度最宽子元素宽度，高度子元素高度相加总和。
 */
export class QUIVerticalLayout extends QUIView {
    protected name: string = "QUIVerticalLayout";

    constructor(
        scene: Phaser.Scene,
        manage: QGamePlugin,
        config?: QUIVerticalLayoutConfig
    ) {
        super(scene, manage, config);
        if (config && config.aplpha) {
            this.Alpha = config.aplpha;
        } else {
            this.Alpha = 0;
        }
    }

    calcHeight(): number {
        let height = this.paddingTop + this.paddingBottom;
        for (let i = 0; i < this.childViews.length; i++) {
            const view = this.childViews[i];
            height += view.Height + this.gap;
        }
        if (this.childViews.length > 0) {
            height -= this.gap;
        }
        return height;
    }

    reLayout(): void {
        // this.gap = 10;
        // this.paddingTop = 50;
        const expHeight = this.calcHeight();
        if (expHeight > this.height || this.alignY == "top") {
            let height = this.paddingTop;
            for (let i = 0; i < this.childViews.length; i++) {
                const view = this.childViews[i];
                view.Top = height;
                view.Left = 0;
                if (view.Width > this.width) {
                    view.Width = this.width;
                }
                height += view.Height + this.gap;
            }
            if (this.childViews.length > 0) {
                height -= this.gap;
            }
            this.height = height + this.paddingBottom;
            this.reSize();
        } else {
            if (this.alignY == "bottom") {
                let height = this.height - this.paddingBottom;
                for (let i = this.childViews.length - 1; i >= 0; i--) {
                    const view = this.childViews[i];
                    view.Top = height - view.Height;
                    view.Left = 0;
                    height -= view.Height + this.gap;
                }
                if (this.childViews.length > 0) {
                    height += this.gap;
                }
            }
            if (this.alignY == "center") {
                let height = (this.height - expHeight) / 2;
                for (let i = 0; i < this.childViews.length; i++) {
                    const view = this.childViews[i];
                    view.Top = height;
                    view.Left = 0;
                    height += view.Height + this.gap;
                }
                if (this.childViews.length > 0) {
                    height -= this.gap;
                }
            }
        }
    }

    public remove(view: QUIView, destroy?: boolean): void {
        super.remove(view, destroy);
        this.reLayout();
    }

    add(child: QUIView) {
        super.add(child);
        this.reLayout();
        return this;
    }
}
